using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using XML;

public class TextManager
{
    
    public enum Language { EN, FR, DE };

    private static TextManager _instance;
    private Language _language;

    public Dictionary<string, TextModel> listText;

    // Use this for initialization
    private TextManager()
    {
        listText = new Dictionary<string, TextModel>();
        _language  = Language.EN;
        LoadText();
    }

    public static TextManager Instance()
    {
        if (_instance == null)
            _instance = new TextManager();

        return _instance;
    }

    public void setLanguage(Language value)
    {
        _language = value;
    }
    public Language getLanguage()
    {
        return _language;
    }

    public string GetText(string name)
    {
        if (!listText.ContainsKey(name))
            Debug.Log("Text Not found : " + name);
        return listText[name].GetText(_language);
    }

    private void LoadText()
    {
        DeserializeTextFile(Const.PATH_XML + "text");
    }

    //Deserialize a file
    private void DeserializeTextFile(string path)
    {
        TextAsset txt = (TextAsset)Resources.Load(path);

        InStream stream = new InStream(txt.text);

        string name;
        string textFR = "";
        string textEN = "";
        string textDE = "";

        //Read the xml file
        stream.List("text", delegate(InStream stream2)
        {
            stream2.Attribute("name", out name);
            stream2.Attribute("french", out textFR);
            stream2.Attribute("english", out textEN);
            stream2.Attribute("german", out textDE);

            TextModel txtModel = new TextModel(textFR, textEN, textDE);

            listText.Add(name, txtModel);
        });

    }
     
}
